package mspbots.next.ticket.utils;

import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * com.mspbots.ticket.utils.NumberUtil
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/4/28
 */
public class NumberUtil {

    /**
     * Keep a fixed number of decimals
     * Use a rounding strategy RoundingMode.HALF_UP
     * For example, keep 2 decimal places：123.456789 =》 123.46
     *
     * @param value old double
     * @param scale Reserved decimal number
     * @return new value str
     */
    public static String roundStr(double value, int scale) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(scale);
        nf.setRoundingMode(RoundingMode.HALF_UP);
        return nf.format(value);
    }
}
